//
//  SongerCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 Going. All rights reserved.
//

#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height

#import "SongerCollectionViewCell.h"
#import "CustomerNetworking.h"

#import "SongerInformationCollectionViewCell.h"


@interface SongerCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *array;

@end
@implementation SongerCollectionViewCell

- (void)dealloc
{
    [_collection release];
    [_array release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.array = [NSMutableArray array];
        NSString *string = @"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=46&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.86603&longtitude=121.511";
        [self customerWithString:string];
        [self buildCollectionView];
    }
    return self;
}


- (void)customerWithString:(NSString *)text
{
    [CustomerNetworking customerConnectWithURL:text andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        for (NSDictionary *dic in [dictionary objectForKey:@"data"]) {
            SongerModel *songerModel = [SongerModel songerModelWithDictionary:dic];
            [self.array addObject:songerModel];
        }
        [self.collection reloadData];
    }];
}

- (void)buildCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout];
    
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor clearColor];
    
    [self.collection registerClass:[SongerInformationCollectionViewCell class] forCellWithReuseIdentifier:@"songerInformationCollectionViewCell"];
    
    [self.contentView addSubview:_collection];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SongerInformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"songerInformationCollectionViewCell" forIndexPath:indexPath];
    cell.songerModel = [self.array objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 0, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(105, 135);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushSongerController:[self.array objectAtIndex:indexPath.row]];
}

@end
